<html>
<head>
	<title>Excersise 3</title>
	<link href="styles/js-console.css" rel="stylesheet" />
</head>
<body>
	<div id="js-console"></div>
	<script src="scripts/js-console.js"></script>
	<script>

		var givenText = 'Queen are a British rock Band formed in BaNd London in 1970, originally consisting of Freddie Mercury (lead vocals, piano), Brian May (guitar, vocals), John Deacon (bass guitar), and Roger Taylor (drums, vocals). Queens earliest works were influenced by progressive rock, hard rock and heavy metal, but the band gradually ventured into more conventional and radio-friendly works, incorporating more diverse and innovative styles in their music.';
		jsConsole.writeLine('The given text is:');	
		jsConsole.writeLine(givenText);
		var searchedWord = 'band';
		var sensitive = false;
		jsConsole.writeLine('\n');
		jsConsole.writeLine('The searched word is: '+searchedWord);

		numberOfOccurrences(givenText, searchedWord);
		numberOfOccurrences(givenText, searchedWord, true);

		function numberOfOccurrences(givenText, searchedWord, sensitive) {
			var counter = 0;
			sensitive = sensitive || false;
			if(sensitive === false){
				var lowerGiven = givenText.toLowerCase();	
				var searchedWordArray = lowerGiven.split(searchedWord);

				for (var i = 0; i <searchedWordArray.length-1; i++) {
					counter++;
				};
				jsConsole.writeLine('The word: "'+searchedWord+'" appears '+counter+' times. (case insensitive)')
			}
			else{
				var index = givenText.indexOf(searchedWord);
				while (index >= 0) {
                    counter++;
                    index = givenText.indexOf(searchedWord, index+1);
                }
                jsConsole.writeLine('The word: "'+searchedWord+'" appears '+counter+' times. (case sensitive)')
			}
		}	
	</script>
</body>
</html>
